home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / Time.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  1.9 KB  |  59 lines

  1. package symjava.sql;
  2.  
  3. import java.util.Date;
  4.  
  5. public class Time extends Date {
  6.    public Time(int hour, int minute, int second) {
  7.       super(70, 0, 1, hour, minute, second);
  8.    }
  9.  
  10.    public Time(long time) {
  11.       super(time);
  12.    }
  13.  
  14.    public static Time valueOf(String s) {
  15.       if (s == null) {
  16.          throw new IllegalArgumentException();
  17.       } else {
  18.          int firstColon = s.indexOf(58);
  19.          int secondColon = s.indexOf(58, firstColon + 1);
  20.          if (firstColon > 0 & secondColon > 0 & secondColon < s.length() - 1) {
  21.             int hour = Integer.parseInt(s.substring(0, firstColon));
  22.             int minute = Integer.parseInt(s.substring(firstColon + 1, secondColon));
  23.             int second = Integer.parseInt(s.substring(secondColon + 1));
  24.             return new Time(hour, minute, second);
  25.          } else {
  26.             throw new IllegalArgumentException();
  27.          }
  28.       }
  29.    }
  30.  
  31.    public String toString() {
  32.       int hour = super.getHours();
  33.       int minute = super.getMinutes();
  34.       int second = super.getSeconds();
  35.       String hourString;
  36.       if (hour < 10) {
  37.          hourString = "0" + hour;
  38.       } else {
  39.          hourString = Integer.toString(hour);
  40.       }
  41.  
  42.       String minuteString;
  43.       if (minute < 10) {
  44.          minuteString = "0" + minute;
  45.       } else {
  46.          minuteString = Integer.toString(minute);
  47.       }
  48.  
  49.       String secondString;
  50.       if (second < 10) {
  51.          secondString = "0" + second;
  52.       } else {
  53.          secondString = Integer.toString(second);
  54.       }
  55.  
  56.       return hourString + ":" + minuteString + ":" + secondString;
  57.    }
  58. }
  59.